//
// Created by win10 on 24-11-13.
//

#include <stdio.h>
//1.确定栈的空间大小
#define MAXSIZE 100
#define ERROR -1
//2.创建一个栈的结构体
typedef struct{
  //构建栈的数组
  int data[MAXSIZE];
  //栈顶指针
  int top;
}Stack;

//3.初始化结构体
void initStack(Stack *s)
{
  //栈顶指针移到最下方
  s->top = -1;
}

//4.判断栈是否为空
int isStackEmpty(Stack *s){

return s->top == -1;
}

//5.判断栈是否已满
int isStackFull(Stack *s){
  return s->top == MAXSIZE - 1;
}

//6.入栈的操作
void push(Stack *s, int data) {
  //判断栈是否已满
  if(isStackFull(s)) {
    printf("Stack is full\n");
    return;
  }
  // //指针上移一格
  // s->top++;
  // //数值 插入到 指针指向的数组里
  // s->data[s->top] = data;
  s->data[++s->top] = data;
}

//7.出栈
int pop(Stack *s) {//出栈 哪里 顶部指针指向的位置
  //判断栈是否为空
  if(isStackEmpty(s)) {
    printf("Stack is empty\n");
    return ERROR;
  }

  //删除值并返回
  // int value=s->data[s->top];
  // s->top--;
  // return value;
  return s->data[s->top--];
}

//8.获取栈顶的元素
int peek(Stack *s) {
  if(isStackEmpty(s)) {
    printf("Stack is empty\n");
    return ERROR;
  }
  //通过顶部指针来获取栈顶元素
  return s->data[s->top];
}

//新建一个栈
int main(void){
//栈
Stack s;
//初始化
initStack(&s);

  //入栈
  push(&s, 10);
  push(&s, 20);
  push(&s, 30);

  //出栈
  printf("%d\n",pop(&s));
  printf("%d\n",pop(&s));
  printf("%d\n",pop(&s));
}










